
import { Service } from 'egg';

export default class Video extends Service {
    /**
       *获取用户电影列表
       **/
    public async getVideoList() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            pageNum: Joi.number().required(),
            pageSize: Joi.number().max(21).required(),
            type: Joi.number().valid(1, 2),
            status: Joi.string().allow(''),
            nation: Joi.number(),
            sort: Joi.number(),
            orders: Joi.array().required()
        });
        let { error } = ctx.validate(schema, ctx.request.body);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let { pageSize, pageNum, orders, /* type, status, nation, sort */ } = ctx.request.body;
        pageSize = parseInt(pageSize);
        pageNum = parseInt(pageNum);
        let total = (await app.mysql.get(app.dbName).query('SELECT count(*) AS total FROM video_list'))[0]?.total;

        let data = await app.mysql.get(app.dbName).select('video_list', {
            orders: orders,
            limit: pageSize,
            offset: pageSize * (pageNum - 1),
            where: { status: 1/* , nation, sort */ }
        })
        return {
            code: 200,
            data: {
                total,
                list: data || []
            }
        }
        /* if (status) {
            let sql = `SELECT vid,title,total_qisim,coverImg,type,views,sort,nation FROM video_list WHERE type = ${type}${nation ? ' AND nation = ' + nation : ''}${sort ? ' AND sort LIKE ' + '\'%' + sort + '%\'' : ''} ORDER BY ${status == 'hot' ? 'views' : 'create_time'} DESC LIMIT ${(pageNum - 1) * pageSize},${pageSize}`
            return await app.mysql.query(sql)
        } else {
            let sql = `SELECT vid,title,total_qisim,coverImg,type,sort,nation FROM video_list WHERE ${type ? 'type = '}${nation ? ' AND nation = ' + nation : ''}${sort ? ' AND sort LIKE ' + '\'%' + sort + '%\'' : ''} ORDER BY create_time DESC LIMIT ${(pageNum - 1) * pageSize},${pageSize}`;
            return await app.mysql.query(sql)
        } */
    }
    /**
     * 删除电影
     *  */
    public async deleteVideo() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            vid: Joi.string().required()
        });
        let { error } = ctx.validate(schema, ctx.query);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        }
        let { vid } = ctx.query;
        let res = await app.mysql.get(app.dbName).query(`UPDATE video_list SET STATUS = 2 WHERE vid = '${vid}'`);
        if (res.affectedRows) {
            return {
                code: 200,
                data: 'success'
            }
        }
    }
    /**
     * 查看电影详情
     *  */
    public async getVideoDetail() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            vid: Joi.string().required()
        });
        let { error } = ctx.validate(schema, ctx.query);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        }
        let { vid } = ctx.query;
        let res = await app.mysql.get(app.dbName).select('series', {
            where: { vid, status: 1 }
        });
        return {
            code: 200,
            data: res
        }
    }
    /**
     * 搜素电影
     *  */
    public async searchVideoByQuery() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            query: Joi.string().required()
        });
        let { error } = ctx.validate(schema, ctx.query);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        }
        let { query } = ctx.query;
        let res = await app.mysql.get(app.dbName).query(`SELECT * FROM video_list WHERE title LIKE '%${query}%' ORDER BY create_time DESC LIMIT 0,10`)
        return {
            code: 200,
            data: res
        }
    }
    /**
     * 更新电影信息
     *  */
    public async updateVideoInfo() {
        const { ctx, app } = this;
        /*  const { Joi } = app;
         let schema = Joi.object({
             query: Joi.string().required()
         });
         let { error } = ctx.validate(schema, ctx.query);
         if (error) {
             ctx.status = 403;
             return { error: error.message };
         } */
        let { vid } = ctx.request.body;
        let info = { ...ctx.request.body };
        delete info.vid
        let res = await this.app.mysql.get(app.dbName).update('video_list', info, {
            where: {
                vid
            } //修改查询条件
        })
        return {
            code: 200,
            data: res
        }
    }
    /**
     * 新增电影
     **/
    public async addVideo() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            vid: Joi.string().required(),
            title: Joi.string().required(),
            total_qisim: Joi.number().min(1).required(),
            coverImg: Joi.string().required(),
            qisimList: Joi.array().required(),
            sort: Joi.string(),
            nation: Joi.number(),
            lang: Joi.number(),
            type: Joi.number(),
        });
        let { error } = ctx.validate(schema, ctx.request.body);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let { qisimList, vid } = ctx.request.body;
        let info = { ...ctx.request.body };
        delete info.qisimList
        // let newQisimList = [...qisimList]

        for (const item of qisimList) {
            item.qisim = Number(item.qisim);
            item.url_type = Number(item.url_type);
            let scm = Joi.object({
                qisim: Joi.number().required(),
                url: Joi.string().required(),
                url_type: Joi.number().required(),
            });
            let { error } = ctx.validate(scm, item);
            if (error) {
                ctx.status = 403;
                return { error: error.message };
            } else {
                await app.mysql.get(app.dbName).insert('series', { vid, ...item })
            }
        }
        /* let saveVideoRes = */ await app.mysql.get(app.dbName).insert('video_list', info);
        // console.log(saveVideoRes);
        return {
            code: 200,
            data: 'success'
        }

    }
    /**-
     * 更新集结
     *  */
    public async updaQisimInfo() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            vid: Joi.string().required(),
            list: Joi.array().required()
        });
        let { error } = ctx.validate(schema, ctx.request.body);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let { list, vid } = ctx.request.body;
        for (const item of list) {
            item.qisim = Number(item.qisim);
            item.url_type = Number(item.url_type);
            let scm = Joi.object({
                qisim: Joi.number().required(),
                url: Joi.string().required(),
                url_type: Joi.number().required(),
                create_time: Joi.string(),
                vid: Joi.string(),
                id: Joi.number(),
                status: Joi.number(),
            });
            let { error } = ctx.validate(scm, item);
            if (error) {
                ctx.status = 403;
                return { error: error.message };
            } else {
                if (item.id) {
                    await app.mysql.get(app.dbName).update('series', {
                        qisim: item.qisim, url_type: item.url_type, url: item.url
                    }, {
                        where: {
                            id: item.id
                        }
                    })
                } else {
                    await app.mysql.get(app.dbName).insert('series', { ...item, vid })
                }
            }
        }
        return {
            code: 200,
            data: 'success'
        }
    }
    /**
     * 删除某一集
     * */
    public async deleteQisim() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            id: Joi.number().required(),
        });
        let { error } = ctx.validate(schema, ctx.query);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        await app.mysql.get(app.dbName).update('series', { status: 2 }, {
            where: {
                id: ctx.query.id
            }
        })
        return {
            code: 200,
            data: 'success'
        }
    }
}